package com.erp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.dao.ApplyDAO;
import com.erp.dao.UserDAO;
import com.erp.domain.TApply;
import com.erp.dto.Apply;
import com.erp.dto.User;

@Service(value = "applyService")
@Transactional
public class ApplyService {

	@Resource(name = "applyDAO")
	ApplyDAO applyDAO;

	@Resource(name = "userDAO")
	private UserDAO userDAO;

	/**
	 * 根据表单ID查询出表单信息
	 * 
	 * @param formId
	 */
	public Apply getById(Integer formId) {
		return new Apply(applyDAO.getById(formId));
	}

	/**
	 * 查询出用户的所有申请历史
	 * 
	 * @param username
	 * @return
	 */
	public List<Apply> listHistoricApplyByUser(User applicant) {
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put("tApplicant", userDAO.getById(applicant.getId()));
		String hql = "from TApply e where e.tApplicant=:tApplicant order by e.applyTime desc";
		List<TApply> tApplyList = applyDAO.list(hql, mapping);
		List<Apply> applyList = null;
		if (tApplyList != null) {
			applyList = new ArrayList<Apply>();
			for (TApply tApply : tApplyList) {
				applyList.add(new Apply(tApply));
			}
		}
		return applyList;
	}

	/**
	 * 获取用户的代办任务
	 * 
	 * @param username
	 * @return
	 */
	public List<Apply> listApplyByIdList(List<Integer> idList) {

		List<TApply> tApplyList = applyDAO.list(idList);
		List<Apply> applyList = null;
		if (tApplyList != null) {
			applyList = new ArrayList<Apply>();
			for (TApply tApply : tApplyList) {
				applyList.add(new Apply(tApply));
			}
		}
		return applyList;
	}

}
